/**
 * Title: HttpUtil.java
 * Description:
 * author: kotomifi
 * date: Jul 13, 2014
 * ©2014 apping 社区
 */
package com.apping.demo.bbs.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 *
 */
public class HttpUtil {

	public HttpUtil() {
		
	}
	
	public static String getJsonContent(String url, String params) {
		try {
			 String urlName = url + "?" + params;
			 URL realUrl = new URL(urlName);
			 // 打开和URL之间的连接
			 HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
			 // 设置通用的请求属性
			 conn.setConnectTimeout(3000);
			 conn.setDoInput(true);
			 int code = conn.getResponseCode();
			 if (code == 200) {
				 return changeInputStream(conn.getInputStream());
			 }
			 
		 } catch (Exception e) {
			 System.out.println("发送GET请求出现异常!" + e);
			 e.printStackTrace();
		 }
		return "";
	}
		 

	/**
	 * 
	 */
	private static String changeInputStream(InputStream inputStream) {
		// TODO Auto-generated method stub
		String jsonStr = "";
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		int len = 0;
		byte[] data = new byte[1024];
		try {
			while( (len = inputStream.read(data)) != -1) {
				outputStream.write(data, 0, len);
			}
			jsonStr = new String(outputStream.toByteArray());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonStr;
	}
}
